package com.hcb.micro.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcb.micro.controller.base.BaseControllers;
import com.hcb.micro.dto.Category;
import com.hcb.micro.dto.CategoryMall;
import com.hcb.micro.dto.CategorySecondLevel;
import com.hcb.micro.dto.Mall;
import com.hcb.micro.dto.Manager;
import com.hcb.micro.service.ICategoryMallService;
import com.hcb.micro.service.ICategorySecondLevelService;
import com.hcb.micro.service.ICategoryService;
import com.hcb.micro.service.IMallService;
import com.hcb.micro.service.IQqbsAccountService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("category")
public class CategoryController extends BaseControllers{
	
	@Autowired
	IQqbsAccountService qqbsAccountService;
	
	@Autowired
	IMallService mallService;
	
	@Autowired
	ICategoryMallService categoryMallService;
	
	@Autowired
	ICategoryService categoryService;
	
	@Autowired
	ICategorySecondLevelService categorySecondLevelService;
	
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public String list() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		Map<String,Object> para = new HashMap<>();
		if (bodyInfo.get("categoryId") != null && !"".equals(bodyInfo.get("categoryId"))) {
			para.put("categoryId", bodyInfo.getString("categoryId"));
		}
		if (bodyInfo.get("name") != null && !"".equals(bodyInfo.get("name"))) {
			para.put("name", bodyInfo.getString("name"));
		}
		
		List<Category> categoryAllList = categoryService.selectAllByPara(para);
		
		if(bodyInfo.get("page") != null && !"".equals(bodyInfo.get("page"))) {
			para.put("start", (Integer.valueOf(bodyInfo.getString("page"))-1)*Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		if(bodyInfo.get("pageSize") != null && !"".equals(bodyInfo.get("pageSize"))) {
			para.put("size", Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		List<Category> categoryList = categoryService.selectAllByPara(para);
		
		if(categoryList.size() <= 0){
			json.put("result", "1");
			json.put("description", "分类列表不存在");
			return buildReqJsonObject(json);
		}
		List<Map<String,String>> list = new ArrayList<>();
		for(Category category:categoryList){
			Map<String,String> map = new HashMap<>();
			
			map.put("id", category.getId()+"");
			map.put("name", category.getName()+"");
			map.put("image", category.getImage()==null?"":category.getImage());
			map.put("desc", category.getDesc()==null?"":category.getDesc());
			list.add(map);
		}
		
    	json.put("result", "0");
		json.put("description", "获取分类列表成功");
		json.put("list", list);
		json.put("count", categoryAllList.size());
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "detail", method = RequestMethod.POST)
	@ResponseBody
	public String detail() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("categoryId") == null || "".equals(bodyInfo.get("categoryId"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		Category category = categoryService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("categoryId")));
		
		if(category == null){
			json.put("result", "1");
			json.put("description", "分类信息不存在");
			return buildReqJsonObject(json);
		}
		Map<String,Object> map = new HashMap<>();
		
		map.put("id", category.getId()+"");
		map.put("name", category.getName()+"");
		map.put("image", category.getImage()==null?"":category.getImage());
		map.put("desc", category.getDesc()+"");
		
		/*List<String> mallUuidList = categoryMallService.selectMallUuidListByCategoryId(category.getId());
		int haveMall = 0;
		if(mallUuidList.size()>0){
			haveMall = 1;
			
			List<Map<String,Object>> mallList = new ArrayList<>();
			for(String mallUuid:mallUuidList){
				Mall mall = mallService.selectByMallUuid(mallUuid);
				
				Map<String,Object> mallMap = new HashMap<>();
				mallMap.put("mallUuid", mall.getMallUuid());
				mallMap.put("mallName", mall.getMallName());
				mallList.add(mallMap);
			}
			map.put("mallList", mallList);
		}
		map.put("haveMall", haveMall);*/
		
		//商城列表
        Map<String,Object> para = new HashMap<>();
		List<Mall> mallList = mallService.selectByPara(para);
        List<Map<String,Object>> list2 = new ArrayList<>();
        for (Mall m: mallList)
        {	
        	Map<String,Object> mallmap = new HashMap<>();
        	para.put("categoryId", category.getId());
        	para.put("mallUuid", m.getMallUuid());
        	CategoryMall cm = categoryMallService.selectByCategoryIdAndMallUuid(para);
        	if(cm != null){
        		mallmap.put("hava", 1);
        	}else{
        		mallmap.put("hava", 0);
        	}
        	mallmap.put("mallUuid", m.getMallUuid());
        	mallmap.put("mallName", m.getMallName());
        	list2.add(mallmap);
        }
        map.put("mallList", list2);
		
    	json.put("result", "0");
		json.put("description", "获取分类信息成功");
		json.put("category", map);
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "create", method = RequestMethod.POST)
	@ResponseBody
	public String create() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		net.sf.json.JSONArray mallList = bodyInfo.getJSONArray("mallList");
		
		int right = 1;
		if(bodyInfo.get("name") == null || "".equals(bodyInfo.get("name"))){
			right = 0;
		}else if(bodyInfo.get("desc") == null || "".equals(bodyInfo.get("desc"))){
			right = 0;
		}else if(bodyInfo.get("image") == null || "".equals(bodyInfo.get("image"))){
			right = 0;
		}else if(mallList.size() == 0 || mallList == null) {
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		Category category = new Category();
		
		category.setName(bodyInfo.getString("name"));
		category.setImage(bodyInfo.getString("image"));
		category.setDesc(bodyInfo.getString("desc"));
		
		Integer insertCategory = categoryService.insertSelective(category);
		if(insertCategory > 0){
			for(int i=0;i<mallList.size();i++){
				if(mallList.get(i) != null){
					Mall mall = mallService.selectByMallUuid(mallList.get(i).toString());
	        		CategoryMall cm = new CategoryMall();
	        		cm.setMallUuid(mall.getMallUuid());
	        		cm.setCategoryId(category.getId());
	        		cm.setStatus((byte)1);
	        		cm.setCreateTime(new Date());
	        		Integer insertPM = categoryMallService.insertSelective(cm);
	        		if(insertPM == 0){
	        			System.out.println("---新增分类销售范围失败---");
	        			json.put("result", "1");
	        			json.put("description", "新增分类销售范围失败");
	        			return buildReqJsonObject(json);
	        		}else{
	        			System.out.println("---新增分类销售范围成功---");
	        		}
				}
			}
		}else{
			System.out.println("---新增分类失败---");
			json.put("result", "0");
			json.put("description", "新增分类失败");
			return buildReqJsonObject(json);
		}
		
    	json.put("result", "0");
		json.put("description", "新增分类成功");
		json.put("categoryId", category.getId());
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "modify", method = RequestMethod.POST)
	@ResponseBody
	public String modify() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		net.sf.json.JSONArray mallList = bodyInfo.getJSONArray("mallList");
		
		int right = 1;
		if(bodyInfo.get("categoryId") == null || "".equals(bodyInfo.get("categoryId"))){
			right = 0;
		}else if(bodyInfo.get("name") == null || "".equals(bodyInfo.get("name"))){
			right = 0;
		}else if(bodyInfo.get("image") == null || "".equals(bodyInfo.get("image"))){
			right = 0;
		}else if(bodyInfo.get("desc") == null || "".equals(bodyInfo.get("salesPrice"))){
			right = 0;
		}else if(mallList.size() == 0 || mallList == null) {
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		Category category = categoryService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("categoryId")));
		
		if(category == null){
			json.put("result", "1");
			json.put("description", "分类不存在");
			return buildReqJsonObject(json);
		}
		
		category.setName(bodyInfo.getString("name"));
		category.setImage(bodyInfo.getString("image"));
		category.setDesc(bodyInfo.getString("desc"));
		
		Integer updateCategory = categoryService.updateByPrimaryKeySelective(category);
		if(updateCategory > 0){
			System.out.println("---修改分类信息成功---");
			
			Map<String,Object> para = new HashMap<>();
        	para.put("categoryId", category.getId());
    		List<CategoryMall> cmList = categoryMallService.selectByCategoryId(para);
    		if(cmList.size()>0){
    			for(CategoryMall cm:cmList){
					Integer deleteCM = categoryMallService.deleteByPrimaryKey(cm.getId());
	        		if(deleteCM == 0){
	        			System.out.println("---删除分类销售范围失败---");
	        			json.put("result", "1");
	        			json.put("description", "删除分类销售范围失败");
	        			return buildReqJsonObject(json);
	        		}else{
	        			System.out.println("---删除分类销售范围成功---");
	        		}
    			}
    		}
			
			if(mallList.size()>0){
				for(int i=0;i<mallList.size();i++){
					if(mallList.get(i) != null){
						Mall mall = mallService.selectByMallUuid(mallList.get(i).toString());
	        			CategoryMall cm = new CategoryMall();
	        			cm.setMallUuid(mall.getMallUuid());
		        		cm.setCategoryId(category.getId());
		        		cm.setStatus((byte)1);
		        		cm.setCreateTime(new Date());
		        		Integer insertPM = categoryMallService.insertSelective(cm);
		        		if(insertPM == 0){
		        			System.out.println("---新增分类销售范围失败---");
		        			json.put("result", "1");
		        			json.put("description", "新增分类销售范围失败");
		        			return buildReqJsonObject(json);
		        		}else{
		        			System.out.println("---新增分类销售范围成功---");
		        		}
					}
				}
			}
		}else{
			System.out.println("---修改分类失败---");
			json.put("result", "0");
			json.put("description", "修改分类失败");
			return buildReqJsonObject(json);
		}
		
    	json.put("result", "0");
		json.put("description", "修改分类成功");
		json.put("categoryId", category.getId());
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public String delete() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("categoryId") == null || "".equals(bodyInfo.get("categoryId"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		Category category = categoryService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("categoryId")));
		
		if(category == null){
			json.put("result", "1");
			json.put("description", "分类不存在");
			return buildReqJsonObject(json);
		}
		
		Integer rs = categoryService.deleteByPrimaryKey(category.getId());
		if(rs != 1){
			System.out.println("---删除分类失败---");
			json.put("result", "0");
			json.put("description", "删除分类失败");
			return buildReqJsonObject(json);
		}
		
    	json.put("result", "0");
		json.put("description", "删除分类成功");
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "listOfMall", method = RequestMethod.POST)
	@ResponseBody
	public String listOfMall() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		net.sf.json.JSONArray mallList = bodyInfo.getJSONArray("mallList");
		
		int right = 1;
		if(mallList.size() == 0 || mallList == null) {
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		List<Category> categoryAllList = new ArrayList<>();
		Map<String,Object> para = new HashMap<>();
		for(int i=0;i<mallList.size();i++){
			if(mallList.get(i) != null){
				para.put("mallUuid", mallList.get(i).toString());
				List<Category> categoryList = categoryService.selectAllByPara(para);
				if(categoryList.size()>0){
					categoryAllList.addAll(categoryList);
				}
			}
		}
		
		if(categoryAllList.size() <= 0){
			json.put("result", "1");
			json.put("description", "分类列表不存在");
			return buildReqJsonObject(json);
		}
		List<Map<String,String>> list = new ArrayList<>();
		for(Category category:categoryAllList){
			Map<String,String> map = new HashMap<>();
			
			map.put("id", category.getId()+"");
			map.put("name", category.getName()+"");
			map.put("desc", category.getDesc()==null?"":category.getDesc());
			list.add(map);
		}
		
    	json.put("result", "0");
		json.put("description", "获取分类列表成功");
		json.put("list", list);
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "listOfSecondLevel", method = RequestMethod.POST)
	@ResponseBody
	public String listOfSecondLevel() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("categoryId") == null || "".equals(bodyInfo.get("categoryId"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		Category category = categoryService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("categoryId")));
		
		if(category == null){
			json.put("result", "1");
			json.put("description", "分类不存在");
			return buildReqJsonObject(json);
		}
		
		Map<String,Object> para = new HashMap<>();
		if (bodyInfo.get("categoryId") != null && !"".equals(bodyInfo.get("categoryId"))) {
			para.put("categoryId", bodyInfo.getString("categoryId"));
		}
		if (bodyInfo.get("name") != null && !"".equals(bodyInfo.get("name"))) {
			para.put("name", bodyInfo.getString("name"));
		}
		
		List<CategorySecondLevel> cslAllList = categorySecondLevelService.selectByPara(para);
		
		if(bodyInfo.get("page") != null && !"".equals(bodyInfo.get("page"))) {
			para.put("start", (Integer.valueOf(bodyInfo.getString("page"))-1)*Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		if(bodyInfo.get("pageSize") != null && !"".equals(bodyInfo.get("pageSize"))) {
			para.put("size", Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		List<CategorySecondLevel> cslList = categorySecondLevelService.selectByPara(para);
		
		if(cslList.size() <= 0){
			json.put("result", "1");
			json.put("description", "二级分类列表不存在");
			return buildReqJsonObject(json);
		}
		List<Map<String,String>> list = new ArrayList<>();
		for(CategorySecondLevel csl:cslList){
			Map<String,String> map = new HashMap<>();
			
			map.put("id", csl.getId()+"");
			map.put("categoryId", csl.getCategoryId()+"");
			map.put("name", csl.getName()+"");
			list.add(map);
		}
		
    	json.put("result", "0");
		json.put("description", "获取二级分类列表成功");
		json.put("list", list);
		json.put("count", cslAllList.size());
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "createSecondLevel", method = RequestMethod.POST)
	@ResponseBody
	public String createSecondLevel() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("categoryId") == null || "".equals(bodyInfo.get("categoryId"))){
			right = 0;
		}else if(bodyInfo.get("name") == null || "".equals(bodyInfo.get("name"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		Category category = categoryService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("categoryId")));
		
		if(category == null){
			json.put("result", "1");
			json.put("description", "分类不存在");
			return buildReqJsonObject(json);
		}
		
		CategorySecondLevel csl = new CategorySecondLevel();
		csl.setCategoryId(category.getId());
		csl.setName(bodyInfo.getString("name"));
		
		Integer insertCsl = categorySecondLevelService.insertSelective(csl);
		if(insertCsl == 0){
			System.out.println("---新增二级分类失败---");
			json.put("result", "0");
			json.put("description", "新增二级分类失败");
			return buildReqJsonObject(json);
		}
		
    	json.put("result", "0");
		json.put("description", "新增二级分类成功");
		json.put("id", csl.getId());
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "detailOfSecondLevel", method = RequestMethod.POST)
	@ResponseBody
	public String detailOfSecondLevel() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("id") == null || "".equals(bodyInfo.get("id"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		CategorySecondLevel csl = categorySecondLevelService.selectByPrimaryKey(Integer.valueOf(bodyInfo.get("id")+""));
		if(csl == null){
			json.put("result", "1");
			json.put("description", "二级分类不存在");
			return buildReqJsonObject(json);
		}
		
		Map<String,Object> map = new HashMap<>();
		
		map.put("id", csl.getId()+"");
		map.put("categoryId", csl.getCategoryId()+"");
		map.put("name", csl.getName()+"");
		
    	json.put("result", "0");
		json.put("description", "获取二级分类信息成功");
		json.put("categorySecondLevel", map);
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "modifySecondLevel", method = RequestMethod.POST)
	@ResponseBody
	public String modifySecondLevel() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("id") == null || "".equals(bodyInfo.get("id"))){
			right = 0;
		}else if(bodyInfo.get("name") == null || "".equals(bodyInfo.get("name"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		CategorySecondLevel csl = categorySecondLevelService.selectByPrimaryKey(Integer.valueOf(bodyInfo.get("id")+""));
		if(csl == null){
			json.put("result", "1");
			json.put("description", "二级分类不存在");
			return buildReqJsonObject(json);
		}
		csl.setName(bodyInfo.getString("name"));
		
		Integer insertCsl = categorySecondLevelService.updateByPrimaryKeySelective(csl);
		if(insertCsl == 0){
			System.out.println("---修改二级分类失败---");
			json.put("result", "0");
			json.put("description", "修改二级分类失败");
			return buildReqJsonObject(json);
		}
		
    	json.put("result", "0");
		json.put("description", "修改二级分类成功");
		json.put("id", csl.getId());
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "deleteSecondLevel", method = RequestMethod.POST)
	@ResponseBody
	public String deleteSecondLevel() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("id") == null || "".equals(bodyInfo.get("id"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		CategorySecondLevel csl = categorySecondLevelService.selectByPrimaryKey(Integer.valueOf(bodyInfo.get("id")+""));
		if(csl == null){
			json.put("result", "1");
			json.put("description", "二级分类不存在");
			return buildReqJsonObject(json);
		}
		
		Integer delete = categorySecondLevelService.deleteByPrimaryKey(csl.getId());
		if(delete == 0){
			System.out.println("---删除二级分类失败---");
			json.put("result", "0");
			json.put("description", "删除二级分类失败");
			return buildReqJsonObject(json);
		}
		
    	json.put("result", "0");
		json.put("description", "删除二级分类成功");
		json.put("id", csl.getId());
		return buildReqJsonObject(json);
	}
}
